package com.cskaoyan.service;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.GetPermissionsRoleVO;
import com.cskaoyan.bean.vo.MarketRoleVO;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketAdminPermissionMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/6 20:36
 * @Description:
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    MarketAdminPermissionMapper marketAdminPermissionMapper;
    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Autowired
    MarketPermissionMapper marketPermissionMapper;

    @Override
    public CommonData<MarketRoleVO> optionsRole() {
        MarketRoleExample example = new MarketRoleExample();
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        List<MarketRoleVO> marketRoleVOList = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            MarketRoleVO marketRoleVO = new MarketRoleVO();
            marketRoleVO.setValue(marketRole.getId());
            marketRoleVO.setLabel(marketRole.getName());
            marketRoleVOList.add(marketRoleVO);
        }
        PageInfo pageInfo = new PageInfo(marketRoleVOList);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketRole> listRole(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        MarketRoleExample example = new MarketRoleExample();
        example.setOrderByClause(sort + " " + order);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketRoles);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketRole createRole(MarketRole marketRole) {
        marketRole.setAddTime(new Date());
        marketRole.setUpdateTime(new Date());
        int rows = marketRoleMapper.insertSelective(marketRole);
        MarketRole marketRole1 = marketRoleMapper.selectByPrimaryKey(marketRole.getId());
        return marketRole1;
    }

    @Override
    public boolean isDuplicateName(String name) {
        MarketRoleExample example = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(example);
        return marketRoles.size() == 0 ? false : true;
    }

    @Override
    public boolean updateRole(MarketRole marketRole) {
        int rows = marketRoleMapper.updateByPrimaryKeySelective(marketRole);
        return rows > 0 ? true : false;
    }

    @Override
    public boolean deleteRole(MarketRole marketRole) {
        int rows = marketRoleMapper.deleteByPrimaryKey(marketRole.getId());
        return rows > 0 ? true : false;
    }

    @Override
    public GetPermissionsRoleVO getPermissionsRole(Integer roleId) {
        GetPermissionsRoleVO getPermissionsRoleVO = new GetPermissionsRoleVO();
        // 先封装所有权限信息
        // 拿到一级label
        List<String> moduleLabels = marketAdminPermissionMapper.ListModuleLabel();
        // 封装
        ArrayList<GetPermissionsRoleVO.SystemPermissionsDTO> systemPermissions = new ArrayList<>();
        for (String moduleLabel : moduleLabels) {
            GetPermissionsRoleVO.SystemPermissionsDTO permissionsDTO = new GetPermissionsRoleVO.SystemPermissionsDTO();
            permissionsDTO.setId(moduleLabel);
            permissionsDTO.setLabel(moduleLabel);
            // 封装二级label
            // 先查询
            List<String> typeLabelList = marketAdminPermissionMapper.ListTypeLabel(moduleLabel);
            ArrayList<GetPermissionsRoleVO.SystemPermissionsDTO.ChildrenDTO> children = new ArrayList<>();
            for (String typeLabel : typeLabelList) {
                GetPermissionsRoleVO.SystemPermissionsDTO.ChildrenDTO childrenDTO = new GetPermissionsRoleVO.SystemPermissionsDTO.ChildrenDTO();
                childrenDTO.setId(typeLabel);
                childrenDTO.setLabel(typeLabel);

                ArrayList<GetPermissionsRoleVO.SystemPermissionsDTO.ChildrenDTO.ChildrenDTO1> children1 = new ArrayList<>();
                // 查询剩余的api
                MarketAdminPermissionExample example = new MarketAdminPermissionExample();
                example.createCriteria().andModuleLableEqualTo(moduleLabel).andTypeLableEqualTo(typeLabel);
                List<MarketAdminPermission> marketAdminPermissions = marketAdminPermissionMapper.selectByExample(example);

                for (MarketAdminPermission marketAdminPermission : marketAdminPermissions) {
                    GetPermissionsRoleVO.SystemPermissionsDTO.ChildrenDTO.ChildrenDTO1 childrenDTO1 = new GetPermissionsRoleVO.SystemPermissionsDTO.ChildrenDTO.ChildrenDTO1();
                    childrenDTO1.setApi(marketAdminPermission.getApi());
                    childrenDTO1.setLabel(marketAdminPermission.getApiLable());
                    childrenDTO1.setId(marketAdminPermission.getApiId());
                    children1.add(childrenDTO1);
                }

                childrenDTO.setChildren(children1);
                children.add(childrenDTO);
            }

            permissionsDTO.setChildren(children);

            systemPermissions.add(permissionsDTO);
        }

        getPermissionsRoleVO.setSystemPermissions(systemPermissions);

        // 再封装该用户的权限assignedPermissions

        ArrayList<String> assignedPermissions = new ArrayList<>();
        MarketPermissionExample example = new MarketPermissionExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
        for (MarketPermission marketPermission : marketPermissions) {
            assignedPermissions.add(marketPermission.getPermission());
        }

        getPermissionsRoleVO.setAssignedPermissions(assignedPermissions);

        return getPermissionsRoleVO;
    }

    @Override
    public void postPermissionsRole(Integer roleId, List<String> permissions) {
        MarketPermissionExample example = new MarketPermissionExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        int rows = marketPermissionMapper.deleteByExample(example);
        for (String permission : permissions) {
            MarketPermission record = new MarketPermission();
            record.setRoleId(roleId);
            record.setPermission(permission);
            record.setAddTime(new Date());
            record.setUpdateTime(new Date());
            int rows1 = marketPermissionMapper.insertSelective(record);
        }
    }
}
